package com.huo.util;

import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

@Component
public class FileSplit {

    public  void splitFile(String filePath, int fileCount) throws IOException {
        FileInputStream fis = new FileInputStream(filePath);
        FileChannel inputChannel = fis.getChannel();
        try {
            final long fileSize = inputChannel.size();
            long average = fileSize / fileCount;//平均值
            long bufferSize = 200; //缓存块大小，自行调整
            ByteBuffer byteBuffer = ByteBuffer.allocate(Integer.valueOf(bufferSize + "")); // 申请一个缓存区
            long startPosition = 0; //子文件开始位置
            long endPosition = average < bufferSize ? 0 : average - bufferSize;//子文件结束位置
            for (int i = 0; i < fileCount; i++) {
                if (i + 1 != fileCount) {
                    int read = inputChannel.read(byteBuffer, endPosition);// 读取数据
                    readW:
                    while (read != -1) {
                        byteBuffer.flip();//切换读模式
                        byte[] array = byteBuffer.array();
                        for (int j = 0; j < array.length; j++) {
                            byte b = array[j];
                            if (b == 10 || b == 13) { //判断\n\r
                                endPosition += j;
                                break readW;
                            }
                        }
                        endPosition += bufferSize;
                        byteBuffer.clear(); //重置缓存块指针
                        read = inputChannel.read(byteBuffer, endPosition);
                    }
                }else{
                    endPosition = fileSize; //最后一个文件直接指向文件末尾
                }

                FileOutputStream fos = new FileOutputStream(filePath + (i + 1));
                FileChannel outputChannel = fos.getChannel();
                inputChannel.transferTo(startPosition, endPosition - startPosition, outputChannel);//通道传输文件数据
                outputChannel.close();
                fos.close();
                startPosition = endPosition + 1;
                endPosition += average;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inputChannel.close();
            fis.close();
        }

    }

    public  void redFile(){
        try {
            long start = System.currentTimeMillis();
            File file = new File("G:\\ftpfiles\\20190813\\S01_CIFM21.txt"); //10849 + 10851
            BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);
            String line = "";
            int i = 0;

            while((line = reader.readLine()) != null){
                //System.out.println(line);
                i = i + 1;
            }
            long end = System.currentTimeMillis();
            System.out.println(i);
            System.out.println(end-start);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public  void redSplitFile(){
        try {
            for(int i = 1 ; i <= 10 ; i++){
                long start = System.currentTimeMillis();
                File file = new File("G:\\ftpfiles\\20190813\\S01_CIFM21.txt"+i); //10849 + 10851
                System.out.println(file.getPath());
                BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
                BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);
                String line = "";
                int j = 0;

                while((line = reader.readLine()) != null){
                    //System.out.println(line);
                    j = j + 1;
                }
                long end = System.currentTimeMillis();
                System.out.println(j);
                System.out.println(end-start);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) throws Exception {
       /* long startTime = System.currentTimeMillis();
        splitFile("G:\\ftpfiles\\20190813\\S01_ESFM21.txt",2);
        long endTime = System.currentTimeMillis();
        System.out.println("耗费时间： " + (endTime - startTime) + " ms");*/
        FileSplit fs = new FileSplit();
        fs.redFile();
        //fs.redSplitFile();
    }
}
